package com.cubeofnine.sugar;

import java.util.List;
import java.util.Vector;

import android.view.View;
import android.view.animation.AnimationUtils;

import com.cubeofnine.sugar.activity.export_contact.ExportContactsListActivity;
import com.cubeofnine.sugar.activity.import_contact.ImportContactsListActivity;
import com.cubeofnine.sugar.business.LoginBusiness;
import com.cubeofnine.sugar.inf.HomeInterface;
import com.cubeofnine.utils.ActivityClass;
import com.cubeofnine.utils.ActivityParams;
import android.util.*;

public class ActivityImpt extends ActivityClass implements HomeInterface {
	public static final String SKIP_LOGIN = "SKIP_LOGIN";
	public static final String MENU_IMPORT_CONTACT = "Import Contacts";
	public static final String MENU_EXPORT_CONTACT = "Export Contacts";

	public void onClickHomeButton(View view) {

		try {
			view.startAnimation(AnimationUtils.loadAnimation(this,
					R.anim.image_click));
			// TODO Auto-generated method stub
			createMenuPopup(R.string.label_home_menu, new String[] {
					MENU_IMPORT_CONTACT, MENU_EXPORT_CONTACT, MENU_LOGOUT });
		} catch (Exception e) {
			Log.e(ActivityImpt.class.getName(), "Error", e);
		}
	}

	public void onClickMenuPopupHandler(String menuSelected) {
		if (MENU_IMPORT_CONTACT.equals(menuSelected)) {
			gotoActivity(ImportContactsListActivity.class);
		} else if (MENU_EXPORT_CONTACT.equals(menuSelected)) {
			gotoActivity(ExportContactsListActivity.class);
		} else if (MENU_LOGOUT.equals(menuSelected)) {
			try {
				LoginBusiness loginBusiness = new LoginBusiness(this);
				String sugarUrl = loginBusiness.getDefaultValue().getSugarUrl();
				String sessionId = loginBusiness.getDefaultValue()
						.getSessionId();
				loginBusiness.connectSugarCrmWebService(sugarUrl);
				loginBusiness.logout(sessionId);
			} catch (Exception e) {
				Log.e(ActivityImpt.class.getName(), "Error", e);
			}

			List<ActivityParams> params = new Vector<ActivityParams>();
			params.add(new ActivityParams(SKIP_LOGIN, true));

			gotoActivity(LoginActivity.class, params);
			// closeApplication();
		}
	}
}
